package com.yqy.wordvideodemo.ui;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.shizhefei.fragment.LazyFragment;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.yqy.wordvideodemo.IMainCallback;
import com.yqy.wordvideodemo.IOnBackPressed;
import com.yqy.wordvideodemo.R;

public class VideoFragment extends LazyFragment implements IOnBackPressed {

    private StandardGSYVideoPlayer videoPlayer;

    private OrientationUtils orientationUtils;

    private IMainCallback mainCallback;

    @Override
    public void onAttach(@NonNull Activity activity) {
        super.onAttach(activity);
        mainCallback = (IMainCallback) activity;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.activity_simple_play);

        init();
    }

    private void init() {
        videoPlayer = (StandardGSYVideoPlayer) findViewById(R.id.video_player);

        String source1 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
        videoPlayer.setUp(source1, true, "测试视频");

        //增加封面
        ImageView imageView = new ImageView(getActivity());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.drawable.xxx1);
        videoPlayer.setThumbImageView(imageView);
        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
        //设置返回键
        videoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils(getActivity(), videoPlayer);
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mainCallback != null){
                    mainCallback.setVis(false);
                }
                orientationUtils.resolveByClick();
            }
        });
        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(true);
        //设置返回按键功能
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        videoPlayer.startPlayLogic();
    }

    @Override
    protected void onFragmentStartLazy() {
        super.onFragmentStartLazy();
        videoPlayer.onVideoResume();
    }

    @Override
    protected void onFragmentStopLazy() {
        super.onFragmentStopLazy();
        videoPlayer.onVideoPause();
    }

    @Override
    protected void onDestroyViewLazy() {
        super.onDestroyViewLazy();
        GSYVideoManager.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public boolean onBackPressed() {
        Log.d("多少哈哈", "多少哈哈3");
        if (videoPlayer == null) return false;


        if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            videoPlayer.getFullscreenButton().performClick();
            if (mainCallback != null){
                mainCallback.setVis(true);
            }
            return true;
        }
        //释放所有
        videoPlayer.setVideoAllCallBack(null);
        return false;
    }


}
